package com.bb.blog.rabbitmq.controller;

import com.bb.blog.rabbitmq.config.RabbitMqConfig;
import com.bb.blog.rabbitmq.RabbitMqUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
public class TestController {
    @Autowired(required = false)
    RabbitTemplate rabbitTemplate;

    @GetMapping("/getMq")
    public Object testMq(@RequestParam(defaultValue = "com.bb") String routeKey,
                         @RequestParam(defaultValue = RabbitMqConfig.EXCHANGE_DIRECT) String exchangeName) {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "test message, hello!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        RabbitMqUtil.send(exchangeName, routeKey, map);
        return "ok";

    }

}
